#!/bin/bash
# 功能：嵌入式开发专用zsh配置，增强日志颜色区分和开发工具支持
# 适用：Debian 10+ 嵌入式开发环境，国内网络

set -e

# 检查root权限
if [ "$(id -u)" -ne 0 ]; then
    echo "错误：请使用root权限运行（sudo ./install_embedded_zsh.sh）"
    exit 1
fi

# 1. 安装核心依赖（含嵌入式开发工具）
echo "=== 安装基础依赖 ==="
apt update -qq
apt install -y \
    zsh curl git fonts-powerline \
    build-essential gcc-multilib gdb-multiarch \
    make cmake ninja-build pkg-config  # 嵌入式编译工具链

# 2. 确定当前用户
CURRENT_USER=$(logname 2>/dev/null || echo $SUDO_USER)
[ -z "$CURRENT_USER" ] && CURRENT_USER=$(whoami)
USER_HOME="/home/$CURRENT_USER"

# 3. 设置zsh为默认shell
echo "=== 设置zsh为默认shell ==="
chsh -s $(which zsh) $CURRENT_USER

# 4. 从国内源安装oh-my-zsh
echo "=== 安装oh-my-zsh（国内Gitee源） ==="
OH_MY_ZSH_DIR="$USER_HOME/.oh-my-zsh"
if [ ! -d "$OH_MY_ZSH_DIR" ]; then
    su - $CURRENT_USER -c "git clone --depth=1 https://gitee.com/mirrors/oh-my-zsh.git $OH_MY_ZSH_DIR"
    [ ! -f "$USER_HOME/.zshrc" ] && su - $CURRENT_USER -c "cp $OH_MY_ZSH_DIR/templates/zshrc.zsh-template $USER_HOME/.zshrc"
    su - $CURRENT_USER -c "echo 'export ZSH=$OH_MY_ZSH_DIR' >> $USER_HOME/.zshrc"
else
    echo "oh-my-zsh已安装，跳过"
fi

# 5. 安装开发友好的主题和插件
echo "=== 安装开发增强组件 ==="
# 主题：简洁且显示编译相关信息
P10K_DIR="$OH_MY_ZSH_DIR/custom/themes/powerlevel10k"
[ ! -d "$P10K_DIR" ] && su - $CURRENT_USER -c "git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git $P10K_DIR"

# 插件：开发必备
HIGHLIGHT_DIR="$OH_MY_ZSH_DIR/custom/plugins/zsh-syntax-highlighting"
[ ! -d "$HIGHLIGHT_DIR" ] && su - $CURRENT_USER -c "git clone --depth=1 https://gitee.com/zdharma-continuum/zsh-syntax-highlighting.git $HIGHLIGHT_DIR"

SUGGEST_DIR="$OH_MY_ZSH_DIR/custom/plugins/zsh-autosuggestions"
[ ! -d "$SUGGEST_DIR" ] && su - $CURRENT_USER -c "git clone --depth=1 https://gitee.com/mirrors/zsh-autosuggestions.git $SUGGEST_DIR"

# 6. 核心配置：日志颜色区分 + 嵌入式开发增强
echo "=== 配置嵌入式开发环境 ==="
ZSHRC_FILE="$USER_HOME/.zshrc"
if [ -f "$ZSHRC_FILE" ]; then
    # 基础配置
    sed -i 's/^ZSH_THEME=.*/ZSH_THEME="powerlevel10k\/powerlevel10k"/' "$ZSHRC_FILE"
    sed -i 's/^plugins=.*/plugins=(git make cmake sudo extract zsh-syntax-highlighting zsh-autosuggestions)/' "$ZSHRC_FILE"

    # 嵌入式开发专用配置
    cat << 'EOF' >> "$ZSHRC_FILE"

# ===================== 嵌入式开发增强 =====================
# 日志颜色分级输出（Error:红 Warning:黄 Info:绿）
alias log_error='echo -e "\033[31m[ERROR]\033[0m "'    # 红色ERROR
alias log_warn='echo -e "\033[33m[WARNING]\033[0m "'   # 黄色WARNING
alias log_info='echo -e "\033[32m[INFO]\033[0m "'     # 绿色INFO

# 编译日志过滤（自动标记错误和警告）
alias build='make 2>&1 | sed -e "s/error/\x1b[31merror\x1b[0m/I" -e "s/warning/\x1b[33mwarning\x1b[0m/I"'
alias cbuild='cmake --build . 2>&1 | sed -e "s/error/\x1b[31merror\x1b[0m/I" -e "s/warning/\x1b[33mwarning\x1b[0m/I"'

# 交叉编译工具链快捷别名（根据实际工具链修改）
alias arm-gcc='arm-linux-gnueabihf-gcc'
alias arm-g++='arm-linux-gnueabihf-g++'
alias arm-gdb='gdb-multiarch'

# 常用开发目录跳转
alias cdd='cd ~/develop'          # 开发主目录
alias cdb='cd ~/develop/build'    # 编译目录
alias cds='cd ~/develop/src'      # 源码目录

# 日志搜索增强（高亮关键字）
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'

# 终端颜色配置
export TERM="xterm-256color"

# powerlevel10k主题配置（精简显示，突出开发信息）
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
EOF

    # 配置powerlevel10k为开发模式（精简显示，突出路径和编译状态）
    if [ ! -f "$USER_HOME/.p10k.zsh" ]; then
        su - $CURRENT_USER -c "curl -fsSL https://gitee.com/romkatv/powerlevel10k/raw/master/config/p10k-lean.zsh -o $USER_HOME/.p10k.zsh"
        # 追加开发专用配置
        cat << 'EOF' >> "$USER_HOME/.p10k.zsh"
# 嵌入式开发模式：显示编译环境和分支信息
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status time)
POWERLEVEL9K_STATUS_OK=false  # 只显示错误状态
POWERLEVEL9K_VCS_GIT_ICON=''  # 隐藏Git图标，节省空间
EOF
    fi
fi

# 7. 权限修复
chown -R $CURRENT_USER:$CURRENT_USER "$OH_MY_ZSH_DIR" 2>/dev/null || true
chown $CURRENT_USER:$CURRENT_USER "$ZSHRC_FILE" 2>/dev/null || true
chown $CURRENT_USER:$CURRENT_USER "$USER_HOME/.p10k.zsh" 2>/dev/null || true

echo "=== 安装完成 ==="
echo "特性说明："
echo "1. 日志分级输出：log_error/warn/info 带颜色标记"
echo "2. 编译日志自动高亮错误（红色）和警告（黄色）"
echo "3. 交叉编译工具链快捷别名（可在.zshrc中修改）"
echo "4. 重启终端后生效，主题配置：p10k configure"
